import axios from 'axios'

// axios 配置
axios.defaults.timeout = 10000
axios.defaults.baseURL = '/api/n'

axios.interceptors.request.use(config => {
  return config
}, err => {
  return Promise.reject(err)
})

axios.interceptors.response.use(res => {
  let status = res.data.status
  switch (true) {
    case status === '200':
      return res.data.data
    case status === '401':
      // 尚未登录不提示错误
      return '401'
    case status === '500':
      // 后台错误
      return Promise.reject(res.data.message)
    case status === 3030:
      return '3030'
    case status === 40001:
    case status === 40014:
    case status === 42001:
      return Promise.reject(res.data.message)
    case status >= '400':
      return Promise.reject(res.data.message)
    default:
  }
}, err => {
  return Promise.reject(err)
})

export default axios
